package com.wulinbus.controller.admin;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fancy.common.utils.StringUtil;
import com.wulinbus.model.CreativeModel;
import com.wulinbus.model.ScriptBannerModel;
import com.wulinbus.service.CreativeService;
import com.wulinbus.service.ScriptBannerService;
import com.wulinbus.utils.AdminAuthCheck;

@Controller
@AdminAuthCheck
@RequestMapping("/admin/sptbanner")
public class AdminScriptBannerController {

	@Resource
	private ScriptBannerService scriptBannerService;
	@Resource
	private CreativeService creativeService;
	
	@RequestMapping("/list.html")
	public String list(@CookieValue(value="muid",defaultValue="") String muid,Model model, @RequestParam(defaultValue = "15") Integer pageSize,@RequestParam(defaultValue = "1") Integer pageIndex) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{
			model.asMap().clear();
			Integer start = (pageIndex - 1) * pageSize;
			List<ScriptBannerModel> bannerList = scriptBannerService.getList(start, pageSize);
			model.addAttribute("bannerList",bannerList);
			model.addAttribute("totalCount",scriptBannerService.getListCount());
			model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageIndex", pageIndex);
			return "admin/sptbanner_list";
		}
	}
	
	
	@RequestMapping("/add.html")
	public String add(@CookieValue(value="muid",defaultValue="") String muid,Model model) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{
			model.asMap().clear();
			return "admin/sptbanner_add";
		}
	}
	
	@RequestMapping(value = "/add.json", method = { RequestMethod.POST })
	public void add_post(Model model,ScriptBannerModel banner, HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		scriptBannerService.insert(banner);
		response.getWriter().print("{\"message\":\"操作成功\",\"statusCode\":\"200\"}");
	}
	
	@RequestMapping(value = "/edit.html", method = { RequestMethod.GET })
	public String edit(@CookieValue(value="muid",defaultValue="") String muid,Model model,Integer bid) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{
			model.asMap().clear();
			model.addAttribute("banner", scriptBannerService.getById(bid));
			return "admin/sptbanner_edit";
		}
	}
	
	@RequestMapping(value = "/edit.json", method = { RequestMethod.POST })
	public void edit_post(Model model,ScriptBannerModel banner, HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		scriptBannerService.update(banner);
		response.getWriter().print("{\"message\":\"操作成功\",\"statusCode\":\"200\"}");
	}
	
	@RequestMapping(value = "/delete.json", method = { RequestMethod.POST })
	public void delete(Model model,Integer bid, HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		if(scriptBannerService.delete(bid)>0){
			response.getWriter().print("{\"message\":\"操作成功\",\"statusCode\":\"200\"}");
		}
		else
		{
			response.getWriter().print("{\"message\":\"操作失败\",\"statusCode\":\"300\"}");
		}
		
	}
}
